package com.itheima.inetaddress1;

import java.net.InetAddress;

public class Test {

    public static void main(String[] args) {

        //通过InetAddress类获取本机IP对象和对方IP对象
        try {
            //1.获取本机IP对象
            InetAddress ip1 = InetAddress.getLocalHost();
            System.out.println("本机IP对象：" + ip1);
            System.out.println("本机的主机名：" + ip1.getHostName());
            System.out.println("本机的IP地址：" + ip1.getHostAddress());

            System.out.println("=============================================");

            //2.获取对方IP对象
            InetAddress ip2 = InetAddress.getByName("www.baidu.com");
            System.out.println("百度IP对象：" + ip2);
            System.out.println("百度的主机名：" + ip2.getHostName());
            System.out.println("百度的IP地址：" + ip2.getHostAddress());

            System.out.println("=============================================");

            //3.判断本机与对方主机是否联通
            System.out.println(ip2.isReachable(3000));      //相当于：ping www.baidu.com

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
